{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="row">
                <!--搜索框开始-->
                <div class="col-sm-12">
                    <div class="layui-form layui-form-item">
                        <div class="layui-input-inline layui-form">
                            <select name="role" lay-search="">
                                <option  value="">不限状态</option>
                                <option  value="1">审核中</option>
                                <option  value="2">审核通过</option>
                                <option  value="3">审核失败</option>
                                <option  value="4">等待开奖</option>
                                <option  value="5">下架</option>
                                <option  value="7">人数不够下架</option>
                            </select>
                        </div>
                        <div class="layui-col-md6 layui-input-inline">
                            <input type="text" id="key" class="layui-input search" name="key" value="" placeholder="店铺名/商品名"/>
                        </div>
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit="" lay-filter="LAY-search">立即搜索</button>
                            <button  class="layui-btn layui-btn-normal" id="empty"  lay-submit="" lay-filter="LAY-search">重置</button>
                        </div>
                    </div>
                </div>
                <!--搜索框结束-->
            </div>
            <div class="hr-line-dashed"></div>

            <div class="example-wrap">
                <div class="example">
                    <table class="layui-hide" id="test" lay-filter="demo"></table>
                    <!--图片模板-->
                    <script type="text/html" id="imgTpl">
                        <ul class="img_view">
                            {{# if(d.p_banner.length > 0){ }}
                            <li><img src="{{d.p_banner[0]}}" class="img-circle table-img" onerror="this.src='/static/admin/images/head_default.gif'"/></li>
                            {{# if(d.p_banner.length >= 1){ }}
                            {{# for(var i=1;i<d.p_banner.length;i++){  }}
                            <li style="display:none;"><img src="{{d.p_banner[i]}}" ></li>
                            {{# } }}
                            {{# } }}
                            {{# } }}
                        </ul>
                    </script>

                    <script type="text/html" id="staBar">
                        {{# switch(d.d_status){ case 1: }}
                        审核中
                        {{# break;case 2: }}
                        审核通过
                        {{# break;case 3: }}
                        审核失败
                        {{# break;case 4: }}
                        等待开奖
                        {{# break;case 5: }}
                        下架
                        {{# break;case 7: }}
                        人数不够下架
                        {{# break;} }}
                    </script>
                    <script type="text/html" id="hotBar">
                        <input type="checkbox" value="{{d.d_id}}" lay-skin="switch"  lay-text="加入|取消" lay-filter="OnOff"{{ d.d_country == 2 ? 'checked' : '' }}  >
                    </script>
                    <!--操作模板-->
                    <script type="text/html" id="opeBar">
                        {{# if(d.d_status == 1){ }}
                        <a href="javascript:;" onclick="check('{{d.d_id}}','{{d.shop_id}}')" class="btn btn-primary btn-xs m-r-xs">审核</a>
                        {{# } }}
                        <a href="javascript:;" onclick="wk.confirm('{{d.d_id}}','{:url('treasureDel')}')" class="btn btn-danger btn-xs m-r-xs">删除</a>
                        <a href="javascript:;" onclick="son({{d.d_id}})" class="btn btn-primary btn-xs m-r-xs" >排序</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    var level = "{$level}";
    console.log(level);
    //layui渲染数据表格
    layui.use('table', function () {
        var table = layui.table
            , form = layui.form;
            if (level == 1) {
                table.render({
                    elem: '#test'
                    , url: '{:url("Game/treasure")}'
                    ,response: {
                        statusCode: 220 //成功的状态码，默认：0
                    }
                    , page: true
                    , size: 'lg' //sm小尺寸的表格 lg大尺寸
                    , cellMinWidth: 150
                    , limits: [5, 10, 15, 20, 25, 30]
                    , limit: "{:config('pages')}"
                    , loading: true
                    , id: 'testReload'
                    , cols: [[
                        {type:'numbers', width: 80, title: '序号'}
                        , {field: 'shop_name', width: '', title: '店铺名', align: 'center'}
                        , {field: 'p_name', width: '', title: '商品名', align: 'center'}
                        , {field: 'p_banner', width: 90, title: '商品图片', align: 'center', templet: '#imgTpl'}
                        , {field: 'd_price', width: '', title: '价格', align: 'center'}
                        , {field: 'd_stock', width: '', title: '库存', align: 'center'}
                        , {field: 'd_model',width: '', title: '型号', align: 'center'}
                        , {field: 'd_join_integral', width: '', title: '参加积分', align: 'center'}
                        , {field: 'd_join', width: '', title: '已参加人数', align: 'center'}
                        , {field: 'd_join_num', width: '', title: '开奖参加人数', align: 'center'}
                        , {field: 'd_start_time', width: '', title: '开始时间', align: 'center'}
                        , {field: 'd_end_time', width: '', title: '结束时间', align: 'center'}
                        , {field: 'd_win_code', width: '', title: '开奖码', align: 'center'}
                        , {field: 'user_username', width: '', title: '中奖用户', align: 'center'}
                        , {field: 'd_win_time', width: '', title: '开奖时间', align: 'center'}
                        , { width: '', title: '人数中奖模式', align: 'center',templet:"<div>{{d.d_type == 1 ? '是' : '否'}}</div>"}
                        , {field: 'd_status', width: '', title: '状态', align: 'center',templet:"#staBar"}
                        , {fixed: 'right', width: 80, field: 'd_sort', title: '排序值', align: 'center'}
                        , {fixed: 'right', width: 100, title: '全国推广', align: 'center', templet: '#hotBar'}
                        , {fixed: 'right', width: 200, title: '操作', align: 'center', toolbar: '#opeBar'}
                    ]],
                    done: function (res, curr, count) {
                        $('.img_view').viewer();
                    }
                });
            }else{
                table.render({
                    elem: '#test'
                    , url: '{:url("Game/treasure")}'
                    ,response: {
                        statusCode: 220 //成功的状态码，默认：0
                    }
                    , page: true
                    , size: 'lg' //sm小尺寸的表格 lg大尺寸
                    , cellMinWidth: 150
                    , limits: [5, 10, 15, 20, 25, 30]
                    , limit: "{:config('pages')}"
                    , loading: true
                    , id: 'testReload'
                    , cols: [[
                        {type:'numbers', width: 80, title: '序号'}
                        , {field: 'shop_name', width: '', title: '店铺名', align: 'center'}
                        , {field: 'p_name', width: '', title: '商品名', align: 'center'}
                        , {field: 'p_banner', width: 90, title: '商品图片', align: 'center', templet: '#imgTpl'}
                        , {field: 'd_price', width: '', title: '价格', align: 'center'}
                        , {field: 'd_stock', width: '', title: '库存', align: 'center'}
                        , {field: 'd_model',width: '', title: '型号', align: 'center'}
                        , {field: 'd_join_integral', width: '', title: '参加积分', align: 'center'}
                        , {field: 'd_join', width: '', title: '已参加人数', align: 'center'}
                        , {field: 'd_join_num', width: '', title: '开奖参加人数', align: 'center'}
                        , {field: 'd_start_time', width: '', title: '开始时间', align: 'center'}
                        , {field: 'd_end_time', width: '', title: '结束时间', align: 'center'}
                        , {field: 'd_win_code', width: '', title: '开奖码', align: 'center'}
                        , {field: 'user_username', width: '', title: '中奖用户', align: 'center'}
                        , {field: 'd_win_time', width: '', title: '开奖时间', align: 'center'}
                        , { width: '', title: '人数中奖模式', align: 'center',templet:"<div>{{d.d_type == 1 ? '是' : '否'}}</div>"}
                        , {field: 'd_status', width: '', title: '状态', align: 'center',templet:"#staBar"}
                        , {fixed: 'right', width: 80, field: 'd_sort', title: '排序值', align: 'center'}
                        , {fixed: 'right', width: 200, title: '操作', align: 'center', toolbar: '#opeBar'}
                    ]],
                    done: function (res, curr, count) {
                        $('.img_view').viewer();
                    }
                });
            }
        form.on('switch(OnOff)', function (obj) {
            var num = '';
            obj.elem.checked == true? num = 2: num = 1;
            //用户状态
            wk.status(this.value,num, '{:url("country")}',obj);
        });
    });
    function son(shop_id) {
        layer.prompt({
            formType: 2,
            title: '修改排序值',
            area: ['200px', '50px'],
            btn: ['确认', '取消'],
            maxlength:11,
        },function(value, index, elem){
            var pattern = /^[1-9]\d*$/;
            if(!pattern.test(value)){
                wk.error('请填写正确的排序值');
                return;
            }
            send1(shop_id,value);
            layer.close(index);
        });
    }
    function send1(shop_id,value){
        $.ajax({
            url: "{:url('addSort')}",
            type: "post",
            dataType: "json",
            data: {d_id:shop_id,value:value},
            success: function (res) {
                if(res.code == 200){
                    layer.msg(res.msg,{icon:1,time:1500,shade: 0.1},function(){
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                } else if (res.code == 100){
                    layer.msg(res.msg,{icon:2,time:1500,shade: 0.1});
                }
            }
        });
    }

    function check(d_id,shop_id) {
        layer.prompt({
            formType: 2,
            value: ' ',
            title: '审核是否通过，驳回请填写原因',
            area: ['800px', '350px'],
            btn: ['通过', '驳回'],
            maxlength:100,
            btn2:function(index, elem){
                var value = layui.jquery('#layui-layer'+index + " .layui-layer-input").val();
                if (!value || value == ' ') {
                    wk.error('驳回请填写原因');
                    return false;　　// 如果value为空，停止继续执行
                }
                send(d_id,shop_id,3,value);
                layer.close(index);
            }
        },function(value, index, elem){
            send(d_id,shop_id,2,value);
            layer.close(index);
        });
    }
    function send(d_id,shop_id,type,msg){
        $.ajax({
            url: "{:url('doCheck')}",
            type: "post",
            dataType: "json",
            data: {d_id:d_id,shop_id:shop_id,type:type,msg:msg},
            success: function (res) {
                if(res.code == 200){
                    layer.msg(res.msg,{icon:1,time:1500,shade: 0.1},function(){
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                } else if (res.code == 100){
                    layer.msg(res.msg,{icon:2,time:1500,shade: 0.1});
                }
            }
        });
    }
</script>
</body>
</html>